/**
 *  A Download Tool for web
 */
package com.app;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Jun
 *
 */
public class DownloadApp {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args)  {
		// TODO Auto-generated method stub

//			 String[] meta = new String[2];
//		     filetype("text/html;charset=utf-8", meta);
		DownLoad();
		
	}
	
	static void DownLoad(){
		
		URL yahoo = null;
		BufferedReader in = null;
        BufferedWriter bw = null;
        HttpURLConnection con = null;
        
		try {
			yahoo = new URL("http://www.google.com/#q=测试");
			con = (HttpURLConnection)yahoo.openConnection();
			con.setRequestProperty("User-Agent", "MSIE 7.0");
			
			// Get the htmlType and Text Encoding
			String[] meta = new String[2];			
			filetype(con.getContentType(),meta);
			
			in = new BufferedReader(new InputStreamReader(
			                        con.getInputStream(),meta[1]));

	        String outfile = "E:\\Test\\1."+ meta[0]; 
//	        FileOutputStream out = new FileOutputStream(new File(outfile));
//	        OutputStreamWriter outwriter = new OutputStreamWriter(out, meta[1]);
	        FileWriter fw = new FileWriter(outfile);
	        bw = new BufferedWriter(fw);
	        
	        String l = "";
	        while((l = in.readLine())!= null){        	
	        	bw.write(l);
	        }
	        con.disconnect();
	        
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{			
			try {
				in.close();
				bw.flush();
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}		
	}

	//mata: filetype;encoding
	static void filetype(String type, String[] meta){
		
		String pstr = "text/([a-z]+);\\s*charset=([\\S]+)";
		Pattern p = Pattern.compile(pstr);		
		Matcher m = p.matcher(type);
		
		if(m.find()){
//			int c = m.groupCount();
//			for(int i = 0; i<= c; i++){
//				System.out.println(m.group(i));			
//			}
			meta[0] = m.group(1);
			meta[1] = m.group(2);			
		}		
//		return type.substring(5);
//		return type;
	}
	
	 static void DownLoadPage(){
		   
		   /* from StackOverflow 
		    *
		   URL url;
		   InputStream is = null;
		   DataInputStream dis;
		   String line;

		   try {
		       url = new URL("http://stackoverflow.com/");
		       is = url.openStream();  // throws an IOException
		       dis = new DataInputStream(new BufferedInputStream(is));

		       while ((line = dis.readLine()) != null) {
		           System.out.println(line);
		       }
		   } catch (MalformedURLException mue) {
		        mue.printStackTrace();
		   } catch (IOException ioe) {
		        ioe.printStackTrace();
		   } finally {
		       try {
		           is.close();
		       } catch (IOException ioe) {
		           // nothing to see here
		       }
		   }
		   */
	   }
}

  
